/**
 * FileFind - ftp/shares indexing and search php engine
 * Module: common
 * Description: Common classes
 * Copyright (c) 2003-2010 Rage Steel, www.gt2.ru
 */
package ru.ffind.common.tools;

import java.util.ArrayList;
import java.util.List;

import ru.ffind.common.model.HostPath;

import com.google.common.base.CharMatcher;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;

/**
 * Parser of string to HostPath, to be used in browsing in webapp.
 *
 * @author Rage Steel
 */
public class HostPathParser {
	public static final char PATH_CHAR = '/'; 
	private static final CharMatcher PATH_MATCHER = CharMatcher.is(PATH_CHAR);
	private static final Splitter PATH_SPLITTER = 
		Splitter.on(PATH_MATCHER).omitEmptyStrings();
	
	/**
	 * Parse path.
	 * 
	 * @param path
	 * @return Parsed path of HostPath.EMPTY
	 */
	public static HostPath parse(String path) {
		if (Strings.isNullOrEmpty(path)) {
			return HostPath.EMPTY;
		}
		
		String hostName = null;
		List<String> pathItems = new ArrayList<String>();
		for (String part : PATH_SPLITTER.split(path)) {
			if (null == hostName) {
				hostName = part;
			} else {
				pathItems.add(part);
			}
		}
		
		return HostPath.of(hostName, pathItems);
	}

}
